package com.tim.app.ui.adapter.someone_homepage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.util.BitmapUtil;
import com.application.library.util.TimeUtil;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.ShareInfo;
import com.tim.app.util.BitmapLoader;

import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * 分享列表的适配器
 */
public class HomepageShareAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, ShareInfo> {

    private Context mContext;

    public HomepageShareAdapter(Context mContext, List<ShareInfo> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        switch (viewType) {
            case ShareInfo.TYPE_STORY:
                holder = new StoryViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_homepage_share_story, parent, false));
                break;
            case ShareInfo.TYPE_ARTICLE:
                holder = new ArticleViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_homepage_share_article, parent, false));
                break;
            case ShareInfo.TYPE_SUNJCET:
                holder = new SubjcetViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_homepage_share_subject, parent, false));
                break;
            case ShareInfo.TYPE_QUESTION:
                holder = new QuestionViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_homepage_share_question, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return getDataList().get(position).getType();
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, final int position, final ShareInfo data) {
        if (data == null) {
            return;
        }

        if (null == data)
            return;
        switch (data.getType()) {
            case ShareInfo.TYPE_STORY:
                final StoryViewHolder holder = (StoryViewHolder) mHolder;
                updateShareStoryUi(holder, position, data);
                break;
            case ShareInfo.TYPE_ARTICLE:
                final ArticleViewHolder holderArticle = (ArticleViewHolder) mHolder;
                updateShareArticeUi(holderArticle, position, data);
                break;
            case ShareInfo.TYPE_SUNJCET:
                final SubjcetViewHolder holderSubject = (SubjcetViewHolder) mHolder;
                updateShareSubjectUi(holderSubject, position, data);
                break;
            case ShareInfo.TYPE_QUESTION:
                final QuestionViewHolder holderQuestion = (QuestionViewHolder) mHolder;
                updateShareQuestionUi(holderQuestion, position, data);
                break;
        }

    }

    /**
     * 更新故事类型的UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateShareStoryUi(final StoryViewHolder holder, final int position, final ShareInfo data) {

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvStoryTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvStoryContent.setText(data.getContent());
        }
        holder.tvShareDesc.setText(mContext.getString(R.string.shareDesc, "故事"));
        if (!TextUtils.isEmpty(data.getShareDesc())) {
            holder.tvShareText.setText(data.getShareDesc());
        }
        if (data.getPriseNumber() > 0) {
            holder.tvSharePriseNumber.setText(mContext.getString(R.string.homepageStoryPriseNumber, String.valueOf(data.getPriseNumber())));
        }
        if (data.getCommentNumber() > 0) {
            holder.tvShareCommentNumber.setText(mContext.getString(R.string.homepageStoryCommentNumber, String.valueOf(data.getCommentNumber())));
        }
        holder.rlStory.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        holder.tvShareYearInfo.setText(TimeUtil.getYearFromTime(data.getCtime()));
        holder.tvShareMonthInfo.setText(TimeUtil.getMonthFromTime(data.getCtime()) + "月");
        holder.tvShareTimeInfo.setText(TimeUtil.getShareTime(data.getCtime()));

        if (!TextUtils.isEmpty(data.getCoverImageUrl())) {
            BitmapLoader.ins().loadImage(data.getCoverImageUrl(),  holder.rivStory);
            Glide.with(mContext)
                    .load(data.getCoverImageUrl())
                    .bitmapTransform(new BlurTransformation(mContext, AppKey.RADIUS))//高斯模糊
                    .into(holder.rivStoryBg);
        }

    }

    /**
     * 更新文章类型的UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateShareArticeUi(final ArticleViewHolder holder, final int position, final ShareInfo data) {

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvArticleTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvArticleContent.setText(data.getContent());
        }
        holder.tvShareDesc.setText(mContext.getString(R.string.shareDesc, "文章"));
        if (!TextUtils.isEmpty(data.getShareDesc())) {
            holder.tvShareText.setText(data.getShareDesc());
        }
        if (data.getPriseNumber() > 0) {
            holder.tvSharePriseNumber.setText(mContext.getString(R.string.homepageStoryPriseNumber, String.valueOf(data.getPriseNumber())));
        }
        if (data.getCommentNumber() > 0) {
            holder.tvShareCommentNumber.setText(mContext.getString(R.string.homepageStoryCommentNumber, String.valueOf(data.getCommentNumber())));
        }
        holder.tvShareYearInfo.setText(TimeUtil.getYearFromTime(data.getCtime()));
        holder.tvShareMonthInfo.setText(TimeUtil.getMonthFromTime(data.getCtime()) + "月");
        holder.tvShareTimeInfo.setText(TimeUtil.getShareTime(data.getCtime()));

        if (!TextUtils.isEmpty(data.getCoverImageUrl())) {
            holder.rivArticle.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(data.getCoverImageUrl())
                    .bitmapTransform(new BlurTransformation(mContext, AppKey.RADIUS))//高斯模糊
                    .into(holder.rivArticle);
        } else {
            holder.rivArticle.setVisibility(View.GONE);
        }

    }

    /**
     * 更新话题类型的UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateShareSubjectUi(final SubjcetViewHolder holder, final int position, final ShareInfo data) {

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvSubjectTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvSubjectContent.setText(data.getContent());
        }
        holder.tvShareDesc.setText(mContext.getString(R.string.shareDesc, "话题"));
        if (!TextUtils.isEmpty(data.getShareDesc())) {
            holder.tvShareText.setText(data.getShareDesc());
        }
        if (data.getPriseNumber() > 0) {
            holder.tvSharePriseNumber.setText(mContext.getString(R.string.homepageStoryPriseNumber, String.valueOf(data.getPriseNumber())));
        }
        if (data.getCommentNumber() > 0) {
            holder.tvShareCommentNumber.setText(mContext.getString(R.string.homepageStoryCommentNumber, String.valueOf(data.getCommentNumber())));
        }
        holder.tvShareYearInfo.setText(TimeUtil.getYearFromTime(data.getCtime()));
        holder.tvShareMonthInfo.setText(TimeUtil.getMonthFromTime(data.getCtime()) + "月");
        holder.tvShareTimeInfo.setText(TimeUtil.getShareTime(data.getCtime()));

        if (!TextUtils.isEmpty(data.getCoverImageUrl())) {
            holder.rivSubject.setVisibility(View.VISIBLE);
            BitmapLoader.ins().loadImage(data.getCoverImageUrl(),  holder.rivSubject);
        } else {
            holder.rivSubject.setVisibility(View.GONE);
        }

    }

    /**
     * 更新问题类型的UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateShareQuestionUi(final QuestionViewHolder holder, final int position, final ShareInfo data) {

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvQuestionTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvQuestionContent.setText(data.getContent());
        }
        holder.tvShareDesc.setText(mContext.getString(R.string.shareDesc, "问题"));
        if (!TextUtils.isEmpty(data.getShareDesc())) {
            holder.tvShareText.setText(data.getShareDesc());
        }
        if (data.getPriseNumber() > 0) {
            holder.tvSharePriseNumber.setText(mContext.getString(R.string.homepageStoryPriseNumber, String.valueOf(data.getPriseNumber())));
        }
        if (data.getCommentNumber() > 0) {
            holder.tvShareCommentNumber.setText(mContext.getString(R.string.homepageStoryCommentNumber, String.valueOf(data.getCommentNumber())));
        }
        holder.tvShareYearInfo.setText(TimeUtil.getYearFromTime(data.getCtime()));
        holder.tvShareMonthInfo.setText(TimeUtil.getMonthFromTime(data.getCtime()) + "月");
        holder.tvShareTimeInfo.setText(TimeUtil.getShareTime(data.getCtime()));

    }


    class StoryViewHolder extends BaseRecyclerViewHolder {

        TextView tvShareYearInfo;
        TextView tvShareMonthInfo;
        TextView tvShareTimeInfo;
        TextView tvShareDesc;
        TextView tvShareText;
        RelativeLayout rlStory;
        RoundedImageView rivStoryBg;
        RoundedImageView rivStory;
        TextView tvStoryTitle;
        TextView tvStoryContent;
        TextView tvSharePriseNumber;
        TextView tvShareCommentNumber;

        public StoryViewHolder(View itemView) {
            super(itemView);

            tvShareYearInfo = (TextView) itemView.findViewById(R.id.tvShareYearInfo);
            tvShareMonthInfo = (TextView) itemView.findViewById(R.id.tvShareMonthInfo);
            tvShareTimeInfo = (TextView) itemView.findViewById(R.id.tvShareTimeInfo);
            tvShareDesc = (TextView) itemView.findViewById(R.id.tvShareDesc);
            tvShareText = (TextView) itemView.findViewById(R.id.tvShareText);
            rlStory = (RelativeLayout) itemView.findViewById(R.id.rlStory);
            rivStoryBg = (RoundedImageView) itemView.findViewById(R.id.rivStoryBg);
            rivStory = (RoundedImageView) itemView.findViewById(R.id.rivStory);
            tvStoryTitle = (TextView) itemView.findViewById(R.id.tvStoryTitle);
            tvStoryContent = (TextView) itemView.findViewById(R.id.tvStoryContent);
            tvSharePriseNumber = (TextView) itemView.findViewById(R.id.tvSharePriseNumber);
            tvShareCommentNumber = (TextView) itemView.findViewById(R.id.tvShareCommentNumber);
        }

    }

    class SubjcetViewHolder extends BaseRecyclerViewHolder {

        TextView tvShareYearInfo;
        TextView tvShareMonthInfo;
        TextView tvShareTimeInfo;
        TextView tvShareDesc;
        TextView tvShareText;
        RoundedImageView rivSubject;
        TextView tvSubjectTitle;
        TextView tvSubjectContent;
        TextView tvSharePriseNumber;
        TextView tvShareCommentNumber;

        public SubjcetViewHolder(View itemView) {
            super(itemView);

            tvShareYearInfo = (TextView) itemView.findViewById(R.id.tvShareYearInfo);
            tvShareMonthInfo = (TextView) itemView.findViewById(R.id.tvShareMonthInfo);
            tvShareTimeInfo = (TextView) itemView.findViewById(R.id.tvShareTimeInfo);
            tvShareDesc = (TextView) itemView.findViewById(R.id.tvShareDesc);
            tvShareText = (TextView) itemView.findViewById(R.id.tvShareText);
            rivSubject = (RoundedImageView) itemView.findViewById(R.id.rivSubject);
            tvSubjectTitle = (TextView) itemView.findViewById(R.id.tvSubjectTitle);
            tvSubjectContent = (TextView) itemView.findViewById(R.id.tvSubjectContent);
            tvSharePriseNumber = (TextView) itemView.findViewById(R.id.tvSharePriseNumber);
            tvShareCommentNumber = (TextView) itemView.findViewById(R.id.tvShareCommentNumber);
        }

    }


    class QuestionViewHolder extends BaseRecyclerViewHolder {

        TextView tvShareYearInfo;
        TextView tvShareMonthInfo;
        TextView tvShareTimeInfo;
        TextView tvShareDesc;
        TextView tvShareText;
        TextView tvQuestionTitle;
        TextView tvQuestionContent;
        TextView tvSharePriseNumber;
        TextView tvShareCommentNumber;

        public QuestionViewHolder(View itemView) {
            super(itemView);

            tvShareYearInfo = (TextView) itemView.findViewById(R.id.tvShareYearInfo);
            tvShareMonthInfo = (TextView) itemView.findViewById(R.id.tvShareMonthInfo);
            tvShareTimeInfo = (TextView) itemView.findViewById(R.id.tvShareTimeInfo);
            tvShareDesc = (TextView) itemView.findViewById(R.id.tvShareDesc);
            tvShareText = (TextView) itemView.findViewById(R.id.tvShareText);
            tvQuestionTitle = (TextView) itemView.findViewById(R.id.tvQuestionTitle);
            tvQuestionContent = (TextView) itemView.findViewById(R.id.tvQuestionContent);
            tvSharePriseNumber = (TextView) itemView.findViewById(R.id.tvSharePriseNumber);
            tvShareCommentNumber = (TextView) itemView.findViewById(R.id.tvShareCommentNumber);
        }

    }

    class ArticleViewHolder extends BaseRecyclerViewHolder {

        TextView tvShareYearInfo;
        TextView tvShareMonthInfo;
        TextView tvShareTimeInfo;
        TextView tvShareDesc;
        TextView tvShareText;
        RoundedImageView rivArticle;
        TextView tvArticleTitle;
        TextView tvArticleContent;
        TextView tvSharePriseNumber;
        TextView tvShareCommentNumber;

        public ArticleViewHolder(View itemView) {
            super(itemView);

            tvShareYearInfo = (TextView) itemView.findViewById(R.id.tvShareYearInfo);
            tvShareMonthInfo = (TextView) itemView.findViewById(R.id.tvShareMonthInfo);
            tvShareTimeInfo = (TextView) itemView.findViewById(R.id.tvShareTimeInfo);
            tvShareDesc = (TextView) itemView.findViewById(R.id.tvShareDesc);
            tvShareText = (TextView) itemView.findViewById(R.id.tvShareText);
            rivArticle = (RoundedImageView) itemView.findViewById(R.id.rivArticle);
            tvArticleTitle = (TextView) itemView.findViewById(R.id.tvArticleTitle);
            tvArticleContent = (TextView) itemView.findViewById(R.id.tvArticleContent);
            tvSharePriseNumber = (TextView) itemView.findViewById(R.id.tvSharePriseNumber);
            tvShareCommentNumber = (TextView) itemView.findViewById(R.id.tvShareCommentNumber);
        }

    }

}
